var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    address: {
      id: 0,
      province_id: 0,
      city_id: 0,
      district_id: 0,
      address: '',
      full_region: '',
      name: '',
      mobile: '',
      is_default: 0,
      code: ''
    },
    region: ['重庆市', '重庆市', '江北区'],
    customItem: '全部',
    addressId: 0,
    openSelectRegion: false,
    selectRegionList: [
      { id: 0, name: '省份', parent_id: 1, type: 1 },
      { id: 0, name: '城市', parent_id: 1, type: 2 },
      { id: 0, name: '区县', parent_id: 1, type: 3 }
    ],
    regionType: 1,
    selectRegionDone: false
  },
  bindinputMobile(event) {
    let address = this.data.address;
    address.mobile = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.address;
    address.name = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputCode(event) {
    let address = this.data.address;
    address.code = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputPhone(event) {
    let address = this.data.address;
    address.phone = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress(event) {
    let address = this.data.address;
    address.address = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault() {
    let address = this.data.address;
    address.is_default = !address.is_default;
    this.setData({
      address: address
    });
  },
  getAddressDetail() {
    let that = this;
    if (that.data.addressId == 0) {

    } else {

      util.request(api.queryAddress, { addressId: that.data.addressId }).then(function (res) {
        that.setData({
          address: {
            name: res[0].contactsMan,
            phone: res[0].userTel,
            mobile: res[0].userPhone,
            address: res[0].address,
            code: res[0].postCode,
            is_default: res[0].isDefault
          },
          region: [res[0].province, res[0].city, res[0].area]
        })
      })
      // util.request(api.AddressDetail, { id: that.data.addressId }).then(function (res) {
      //   if (res.errno === 0) {
      //     that.setData({
      //       address: res.data
      //     });
      //   }
      // });

    }
  },
  setRegionDoneStatus() {
    let that = this;
    let doneStatus = that.data.selectRegionList.every(item => {
      return item.id != 0;
    });

    that.setData({
      selectRegionDone: doneStatus
    })

  },
  chooseRegion() {
    let that = this;
    this.setData({
      openSelectRegion: !this.data.openSelectRegion
    });



  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    if (options.id) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    }

    this.getRegionList(1);

  },
  onReady: function () {

  },
  selectRegionType(event) {
    let that = this;
    let regionTypeIndex = event.target.dataset.regionTypeIndex;
    let selectRegionList = that.data.selectRegionList;

    // //判断是否可点击
    if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <= 0)) {
      return false;
    }

    this.setData({
      regionType: regionTypeIndex + 1
    })

    let selectRegionItem = selectRegionList[regionTypeIndex];

    this.getRegionList(selectRegionItem.parent_id);

    this.setRegionDoneStatus();

  },
  selectRegion(event) {
    let that = this;
    let regionIndex = event.target.dataset.regionIndex;
    let regionItem = this.data.regionList[regionIndex];
    let regionType = regionItem.type;
    let selectRegionList = this.data.selectRegionList;
    // selectRegionList[regionType - 1] = regionItem;


    // if (regionType != 3) {
    //   this.setData({
    //     selectRegionList: selectRegionList,
    //     regionType: regionType + 1
    //   })
    //   this.getRegionList(regionItem.id);
    // } else {
    //   this.setData({
    //     selectRegionList: selectRegionList
    //   })
    // }

    //重置下级区域为空
    selectRegionList.map((item, index) => {
      // if (index > regionType - 1) {
      //   item.id = 0;
      //   item.name = index == 1 ? '城市' : '区县';
      //   item.parent_id = 0;
      // }
      // return item;
    });

    // this.setData({
    //   selectRegionList: selectRegionList
    // })


    // that.setData({
    //   regionList: that.data.regionList.map(item => {

    //     //标记已选择的
    //     if (that.data.regionType == item.type && that.data.selectRegionList[that.data.regionType - 1].id == item.id) {
    //       item.selected = true;
    //     } else {
    //       item.selected = false;
    //     }

    //     return item;
    //   })
    // });

    // this.setRegionDoneStatus();

  },
  doneSelectRegion() {
    // if (this.data.selectRegionDone === false) {
    //   return false;
    // }

    // let address = this.data.address;
    // let selectRegionList = this.data.selectRegionList;
    // address.province_id = selectRegionList[0].id;
    // address.city_id = selectRegionList[1].id;
    // address.district_id = selectRegionList[2].id;
    // address.province_name = selectRegionList[0].name;
    // address.city_name = selectRegionList[1].name;
    // address.district_name = selectRegionList[2].name;
    // address.full_region = selectRegionList.map(item => {
    //   return item.name;
    // }).join('');

    // this.setData({
    //   address: address,
    //   openSelectRegion: false
    // });

  },
  cancelSelectRegion() {
    // this.setData({
    //   openSelectRegion: false,
    //   regionType: this.data.regionDoneStatus ? 3 : 1
    // });

  },
  getRegionList(regionId) {
    // let that = this;
    // let regionType = that.data.regionType;
    // that.setData({
    //   regionList: city.citys[0]
    // })
  },
  cancelAddress() {
    wx.navigateTo({
      url: '/pages/ucenter/address/address',
    })
  },
  updateUserAddress() {
    let address = this.data.address;

    if (address.name == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (address.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }
    if (address.mobile.length != 11) {
      util.showErrorToast('请输入正确的手机号码');
      return false;
    }
    if (address.code == '') {
      util.showErrorToast('请输入邮编');
      return false;
    }
    if (address.address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }


    let that = this;
    let body = {
      openId: util.session,
      contactsMan: this.data.address.name,
      userTel: this.data.address.phone,
      userPhone: this.data.address.mobile,
      address: this.data.address.address,
      postCode: this.data.address.code,
      province: this.data.region[0],
      city: this.data.region[1],
      area: this.data.region[2],
      isDefault: this.data.address.is_default,
      addressId: this.data.addressId
    }
    util.request(api.updateUserAddress, body, 'GET').then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1000
      })
      wx.navigateTo({
        url: '/pages/shopping/address/address',
      })
    })
  },
  saveAddress() {
    let address = this.data.address;

    if (address.name == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (address.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }
    if (address.mobile.length != 11){
      util.showErrorToast('请输入正确的手机号码');
      return false;
    }
    if (address.code == '') {
      util.showErrorToast('请输入邮编');
      return false;
    }
    if (address.address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }


    let that = this;
    let body = {
      openId: util.session,
      contactsMan: this.data.address.name,
      userTel: this.data.address.phone,
      userPhone: this.data.address.mobile,
      address: this.data.address.address,
      postCode: this.data.address.code,
      province: this.data.region[0],
      city: this.data.region[1],
      area: this.data.region[2],
      isDefault: this.data.address.is_default
    }
    util.request(api.addUserAddress, body, 'GET').then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1000
      })
      wx.navigateTo({
        url: '/pages/shopping/address/address',
      })
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})